package com.huizhan.web.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.stereotype.Controller;
import org.springframework.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.*;
import javax.servlet.http.*;
import org.springframework.ui.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.data.domain.*;
import org.springframework.data.domain.Sort.*;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.*;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.result.*;
import javax.servlet.ServletException;
import java.io.*;
import java.util.*;
import java.net.*;

import com.google.gson.Gson;
import com.huizhan.web.dao.*;
import com.huizhan.web.model.*;

@Controller
@Api(tags = "Login",description = "登记")
public class LoginController {

    @Autowired
    private RegisterDao dao;

    @Autowired
    private PropertyDao pdao;

    @Autowired
    private SourceDao sdao;

    /**
     * 登记
     * @return
     */
    @GetMapping("login/{id}")
    public ModelAndView View(@PathVariable Long id,HttpServletRequest request)
    {
        ModelAndView map = new ModelAndView();

        try {
            Optional<Register> optional= dao.findById(id);
            String lang=request.getParameterMap().containsKey("lang")? request.getParameter("lang"):"zh-CN";
            String sourceId = request.getParameterMap().containsKey("sourceId")?request.getParameter("sourceId"):"0";
            if(optional.isPresent()){
                Register  model=optional.get();
                List<Property> list= pdao.findByRegisterId(id);
                if(sourceId!="0"){
                    Optional<Source> source = sdao.findById(Long.parseLong(sourceId));
                    if(source.isPresent()){
                        source.get().setHit(source.get().getHit()+1);
                        sdao.save(source.get());
                        map.addObject("url",source.get().getUrl());
                    }else{
                        map.addObject("url", model.getUrl());
                    }
                }else{
                    map.addObject("url", model.getUrl());
                }
                map.addObject("sourceId", sourceId);
                map.addObject("id", id);
                map.addObject("type", model.getType());
                map.addObject("disabled", model.getDisabled());
                map.addObject("project", model.getProjectId());
                map.addObject("list", list);
                map.addObject("english", model.getEnglish());
                map.addObject("lang", lang);
                map.addObject("banner", model.getBanner());
                map.addObject("bannerMoblie", model.getBannerMoblie());
                map.addObject("title",lang.equals("zh-CN")? model.getName():model.getEnglishName());
                
                Gson gson = new Gson();  
                
                map.addObject("json",gson.toJson(list));
                
                map.setViewName("login copy.html");
            }
        }catch (Exception e){
            map.setStatus(HttpStatus.NOT_FOUND);
        }

        return map;
    }
}